package com.yuchuang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuchuang.base.PageInfo;
import com.yuchuang.base.Result;
import com.yuchuang.mapper.CommodityMapper;
import com.yuchuang.model.Commodity;
import com.yuchuang.model.dto.CommodityDto;
import com.yuchuang.model.vo.CommodityVo;
import com.yuchuang.service.CommodityService;
import com.yuchuang.utils.Utils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;
import java.util.Map;

/**
 * @Author:cy Date: 2023-06-16-13:14
 * Description:
 */
@Service
public class CommodityServiceImpl extends ServiceImpl<CommodityMapper, Commodity> implements CommodityService {
    @Autowired
    private CommodityMapper commodityMapper;
    @Override
    public List<CommodityDto> queryAll(HttpServletRequest request){
        QueryWrapper<Commodity> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(request.getParameter("commodityName") != null,"commodity_name",request.getParameter("commodityName"));
        List<CommodityDto> list = commodityMapper.queryAllImplBatis(queryWrapper);
        return list;
    }
    @Override
    public PageInfo selectPageVo(CommodityVo commodityVo){
        Page<Commodity> page = new Page<>(commodityVo.getPageNum(),commodityVo.getPageSize());
        QueryWrapper<Commodity> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(commodityVo.getCommodityName() != null,"commodity_name",commodityVo.getCommodityName());
        IPage<CommodityDto> result = commodityMapper.selectPageVoMapper(page,queryWrapper);
        int pageNums =(int) result.getPages();
        int size =(int) result.getSize();
        int pageNum =(int) result.getCurrent();
        int total =(int) result.getTotal();
        List t = result.getRecords();
        return new PageInfo().setContent(t).setPage(pageNum).setSize(size).setTotal(total).setPages(pageNums);
    }
    @Override
    public int insert(Commodity commodity){
            //获取编号最大值
            QueryWrapper<Commodity> queryWrapper = new QueryWrapper<>();
            queryWrapper.inSql("id","select max(id) from yc_commodity");
            List<Commodity> list = commodityMapper.listMax(queryWrapper);
            String codeStr;
            if(list.size() == 0){
                codeStr = Utils.getCode("YCSC","000001");
            }else{
                 codeStr = Utils.getCode("YCSC",list.get(0).getCommodityCode());
            }
            commodity.setCommodityCode(codeStr);
            int val = commodityMapper.insertImplBatis(commodity);
            return val;
    }
    @Override
    public int update(Map<String,Object> params){
        int val = commodityMapper.updateImplBatis(params);
        return val;
    }
    @Override
    public int delete(Map<String,Object> params){
        int val = commodityMapper.deleteImplBatis(params);
        return val;
    }
}
